package ec.cd.server.gui;

import ec.cd.common.Networking.BodyLocation;
import ec.cd.common.Sensors;
import ec.cd.common.Sensors.SensorData;
import java.util.LinkedList;
import java.util.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;

public class GraphThread extends Thread {
    
    private GraphFrame graphFrame;
    private BodyLocation bodyLocation;
    private Queue<SensorData> sensorDatas;
    private Queue<Integer> contexts;
    private long refTime;
    
    public GraphThread() {
        // TODO Auto-generated constructor stub
        sensorDatas = new LinkedList<SensorData>();
        contexts = new LinkedList<Integer>();
        refTime = System.currentTimeMillis();
    }
    
    public void initialize(BodyLocation bodyLocation) {
        this.bodyLocation = bodyLocation;
    }
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        graphFrame = new GraphFrame(bodyLocation.toString());
        graphFrame.setVisible(true);
        //graphFrame.initializeGraphs();
        
        Thread t = new Thread(new Runnable() {
            
            @Override
            public void run() {
                SensorData sensorData;
                Integer context;
                while (true) {
              
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(GraphThread.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    while ((sensorData = sensorDatas.poll()) != null) {
                        switch (sensorData.sensorType) {
                            case Sensors.ST_ACCELEROMETER:
                                graphFrame.getTraceAccX().addPoint(sensorData.timestamp - refTime, sensorData.values[0]);
                                graphFrame.getTraceAccY().addPoint(sensorData.timestamp - refTime, sensorData.values[1]);
                                graphFrame.getTraceAccZ().addPoint(sensorData.timestamp - refTime, sensorData.values[2]);
                                
                                break;
                            case Sensors.ST_GYROSCOPE:
                                graphFrame.getTraceGyrX().addPoint(sensorData.timestamp - refTime, sensorData.values[0]);
                                graphFrame.getTraceGyrY().addPoint(sensorData.timestamp - refTime, sensorData.values[1]);
                                graphFrame.getTraceGyrZ().addPoint(sensorData.timestamp - refTime, sensorData.values[2]);
                                
                                break;
                        }
                    }
                    while ((context = contexts.poll()) != null) {
                        graphFrame.getTraceState().addPoint(System.currentTimeMillis() - refTime, context);
                    }
                }
            }
        });
        t.start();
        
    }
    
    public void addSensorData(SensorData sensorData) {
        sensorDatas.add(sensorData);
    }
    
    public void addContext(int context) {
        contexts.add(context);
    }

    /**
     * @return the sensorDatas
     */
    public Queue<SensorData> getSensorDatas() {
        return sensorDatas;
    }

    /**
     * @param sensorDatas the sensorDatas to set
     */
    public void setSensorDatas(Queue<SensorData> sensorDatas) {
        this.sensorDatas = sensorDatas;
    }

    /**
     * @return the contexts
     */
    public Queue<Integer> getContexts() {
        return contexts;
    }

    /**
     * @param contexts the contexts to set
     */
    public void setContexts(Queue<Integer> contexts) {
        this.contexts = contexts;
    }
}
